//
//  RWNormalBillView.m
//  RedWine
//
//  Created by Liufangfang on 2017/1/14.
//  Copyright © 2017年 HuiBangKe. All rights reserved.
//

#import "RWNormalBillView.h"

@interface RWNormalBillView ()<UITextFieldDelegate>{
    
    NSArray *sectionHeaderTitles;
    NSArray *normalBillContents;
}

@end

@implementation RWNormalBillView

#pragma mark - Init Methods

- (instancetype)init{

    self = [super init];
    
    if (self) {
        [self setNormalBillUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    
    if (self) {
        [self setNormalBillUI];
    }
    return self;
}

#pragma mark - Load Data Methods

- (void)initObjects{
    
    sectionHeaderTitles = @[@"发票抬头", @"发票内容"];
    normalBillContents = @[@"酒", @"礼品", @"食品"];
}

#pragma mark - Config UI Methods

- (void)setNormalBillUI{

    [self initObjects];
    
    _normalBillTV = [[FTYTouchTableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    [self addSubview:_normalBillTV];
//    _normalBillTV.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
    _normalBillTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _normalBillTV.scrollEnabled = NO;
    _normalBillTV.dataSource = self;
    _normalBillTV.delegate = self;
    _normalBillTV.touchDelegate = self;
}

#pragma mark - Notificaiton Method

#pragma mark - Notificaiton Invoke Methods

#pragma mark - <UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (0 == section) {
        return 1;
    } else {
        return 3;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 40.*FTY_HEIGHT_BASE;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60.*FTY_HEIGHT_BASE;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *headerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, 50.*FTY_HEIGHT_BASE)];
    headerV.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLab = [[UILabel alloc] init];
    [headerV addSubview:titleLab];
    titleLab.text = sectionHeaderTitles[section];
    titleLab.textColor = [UIColor blackColor];
    titleLab.font = FTY_FONTTSIZE(15);
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(10.*FTY_WIDTH_BASE);
        make.height.mas_equalTo(CGRectGetHeight(headerV.frame));
    }];
    _titleLab = titleLab;
    
    return headerV;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * const billID = @"billID";
    UITableViewCell *billCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:billID];
    billCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSInteger row = indexPath.row;
    NSInteger section = indexPath.section;
    
    switch (section) {
        case 0:{
            
            UITextField *textField = [[UITextField alloc] init];
            [billCell addSubview:textField];
            textField.placeholder = @"个人";
            textField.font = FTY_FONTTSIZE(15);
            textField.textColor = [FTYColor grayColor];
            textField.delegate = self;
            [textField sizeToFit];
            [textField mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(billCell);
                make.left.mas_equalTo(20.*FTY_WIDTH_BASE);
                make.right.mas_offset(-10.*FTY_WIDTH_BASE);
                make.height.mas_equalTo(billCell.mas_height);
            }];
            _textField = textField;
            
            break;
        }
        case 1:{
            
            UIButton *selectedBtn = [[UIButton alloc] init];
            [billCell addSubview:selectedBtn];
            [selectedBtn addTarget:self action:@selector(selectedBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            billCell.tag = row;
            selectedBtn.tag = row;
            //思：此约束与bounds的区别
            [selectedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.left.bottom.right.mas_offset(0);
            }];
            _selectedBtn = selectedBtn;
            
            UILabel *textField = [[UILabel alloc] init];
            [selectedBtn addSubview:textField];
            textField.text = normalBillContents[row];
            textField.font = FTY_FONTTSIZE(15);
            textField.textColor = [FTYColor grayColor];
            [textField sizeToFit];
            [textField mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(billCell);
                make.left.mas_equalTo(20.*FTY_WIDTH_BASE);
                make.right.mas_offset(-10.*FTY_WIDTH_BASE);
                make.height.mas_equalTo(billCell.mas_height);
            }];
        
            //底部线
            UIView *line = [[UIView alloc] init];
            [billCell addSubview:line];
            line.backgroundColor = [UIColor lightGrayColor];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(10.*FTY_WIDTH_BASE);
                make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
                make.bottom.mas_offset(0);
                make.height.mas_offset(.5*FTY_HEIGHT_BASE);
            }];
            
            break;
        }
            
        default:
            break;
    }
    
    return billCell;
}

#pragma mark - <UITableViewDelegate>

#pragma mark - <FTYTouchTableViewDelegate>

- (void)tableView:(UITableView *)tableView touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [_textField resignFirstResponder];
}

#pragma mark - <UITextFieldDelegate>

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [_textField resignFirstResponder];
    
    return YES;
}

#pragma mark - Private Invoke Method

- (void)selectedBtnAction:(UIButton *)btn{

    _selectedBtn.selected = NO;
    btn.selected = YES;
    [btn setImage:[UIImage imageNamed:@"selected_icon"] forState:UIControlStateSelected];
    _selectedBtn = btn;
}

@end
